﻿Type.registerNamespace("Al.UI.Controls");

Al.UI.Controls.CometRequestor = function(element) {
    Al.UI.Controls.CometRequestor.initializeBase(this, [element]);
    //class properties
    this._pollTime = null;
    this._sessionID = null;
    this._postbackQueue = null;
    this._argsQueue = null;
    this._isCallingHandler = false;
    this._onRecieveHandler = null;
}

//
//Create the Prototype of the control
//
Al.UI.Controls.CometRequestor.prototype = {
    initialize: function() {
        Al.UI.Controls.CometRequestor.callBaseMethod(this, 'initialize');

        this._postbackQueue = new Array();
        this._argsQueue = new Array();
        this._isCallingHandler = false;

        this._onInitializeAjaxRequestHandler = Function.CreateDelegate(this, this._onInitializeAjaxRequest);
        this._onEndAjaxRequestHandler = Function.CreateDelegate(this, this._onEndAjaxRequest);

        this._callCometHandler = Function.CreateDelegate(this, this.CallComet);

        var prm = Sys.WebForms.PageRequestManager.getInstance();
        prm.add_initializeRequest(this._onInitializeAjaxRequestHandler);
        prm.add_endRequest(this._onEndAjaxRequestHandler);

        this.StartCometPolling();
    },

    dispose: function() {
        Al.UI.Controls.CometRequestor.callBaseMethod(this, 'dispose');
    },

    //
    //Control Properties
    //
    get_pollTime: function() {
        return this._pollTime;
    },

    set_pollTime: function(value) {
        if (this._pollTime != value) {
            this._pollTime = value;
            this.raisePropertyChanged('pollTime');
        }
    },

    get_sessionID: function() {
        return this._sessionID;
    },

    set_sessionID: function(value) {
        if (this._sessionID != value) {
            this._sessionID = value;
            this.raisePropertyChanged('sessionID');
        }
    },

    //
    //Event Handlers
    //
    _onInitializeAjaxRequest: function(sender, args) {
        var prm = Sys.WebForms.PageRequestManager.getInstance();
        if (prm.get_isInAsyncPostback()) {
            args.set_cancel(true);
            this._postbackQueue.push(args.get_postbackElement());
            this._postbackQueue.push(args.get_postbackElement());
        }
    },

    _onEndAjaxRequest: function(sender, args) {
        if (this._postbackQueue.length > 0) {
            __doPostback(this._postbackQueue.shift(), this._argsQueue.shift());
        }
    },

    _onReadyStateChanged: function(e) {
        if (e.xmlHttp.readyState == 4) {
            if (e.xmlHttp.responseText == 'Dirty') {
                __doPostback('cometPostbackButton', '');
            }
            e.sender._isCallingHandler = false;
        }
    },

    //
    //Implementation
    //
    CallComet: function() {
        var cometRequestor = $find('cometRequestor');
        if (!cometRequestor._isCallingHandler) {
            cometRequestor._isCallingHandler = true;
            var cometRequest = new XMLHttpRequest();

            var url = 'CometAsyncHandler.ashx?' + cometRequestor.get_sessionID();
            cometRequestor._onRecieveHandler = Function.createCallback(cometRequestor._onReadystateChange, { sender: cometRequest, xmlHttp: cometRequest });
            cometRequest.onreadystatechange = cometRequestor._onRecieveHandler;
            cometRequest.send();
        }
        setTimeout(cometRequest._callCometHandler, cometRequestor._pollTime);
    },

    StartCometPolling: function() {
        this._callCometHandler();
    }
}

Al.UI.Controls.Repeater.registerClass('Al.UI.Controls.Comet.CometRequestor', Sys.UI.Control);

if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();